package com.example.userlogin.service;

//实现类

import com.example.userlogin.dao.SupplierDao;
import com.example.userlogin.entity.Supplier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service

//实现操作方法
public class SupplierService implements SupplierDao{

    @Autowired
    JdbcTemplate jdbcTemplate;
    @Override
    public int add(Supplier supplier) {
//        创建SQL语句
        String sql = "insert into supplier values(null,?,?,?,?)";
//        调用方法实现
        Object[] args = {supplier.getName(),supplier.getAddress(),supplier.getContact_person(),supplier.getTelephone()};
        return jdbcTemplate.update(sql,args);
    }

    @Override
    public int delete(Supplier supplier) {
        String sql ="delete from supplier where Id=?";
        Object[] args={supplier.getId()};
        return jdbcTemplate.update(sql,args);
    }


    @Override
    public int update(Supplier supplier) {
        String sql ="update supplier set name=?,address=?,contact_person=?,telephone=? where Id=?";
        Object[] args={
                supplier.getName(),supplier.getAddress(),supplier.getContact_person(),
                supplier.getTelephone(),supplier.getId()
        };
        return jdbcTemplate.update(sql,args);
    }

    /**
     *
     * @param
     * @return
     */
    @Override
    public Supplier findOne(Supplier supplier) {
        String sql = "select * from supplier where Id="+supplier.getId().toString();
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<Supplier>(Supplier.class));
    }

    /**
     * 获取所有商品列表的方法
     * @return
     */
    @Override
    public List<Supplier> findAll() {
        String sql = "select * from supplier";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<Supplier>(Supplier.class));
    }
}
